<template>
  <div class="fconfig-container">
    <div class="fconfig-search">
      <el-form :inline="true" :model="searchform" ref="searchform" class="demo-form-inline">
        <el-form-item label="配置查询" prop="key">
          <el-select clearable v-model="searchform.key" placeholder="请选择oss类型" size="small">
            <el-option
              v-for="item in ossTypes"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="listPage">查询</el-button>
          <el-button type="primary" size="mini" @click="resetForm('searchform')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="fconfig-body">
      <el-button type="primary" size="mini" style="margin-bottom: 20px;" @click="addFileConfig">添加配置</el-button>
      <el-table :data="configs" style="width: 100%;margin-bottom: 20px;" row-key="id" border>
        <el-table-column prop="type" label="参数类型"></el-table-column>
        <el-table-column prop="code" label="参数编码"></el-table-column>
        <el-table-column prop="ossUrl" label="上传url"></el-table-column>
        <el-table-column prop="ossAccessKeyId" label="oss存储id"></el-table-column>
        <el-table-column prop="bucket" label="文件桶"></el-table-column>
        <el-table-column prop="status" label="是否启用">
          <template v-slot="{row}">
            <el-switch
              v-model="row.status"
              :active-value="1"
              :inactive-value="0"
              active-color="#13ce66"
              inactive-color="#ff4949"
              @change="handleChange(row)"
            ></el-switch>
          </template>
        </el-table-column>

        <el-table-column label="操作" width="200">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="searchform.pageNum"
        :page-sizes="[5, 10, 15]"
        :page-size="searchform.pageSize"
        layout="total, sizes, prev, pager, next"
        :total="total"
      ></el-pagination>
    </div>

    <!-- 新增 / 编辑 -->
    <add-and-update
      ref="addAndUpdate"
      v-model="drawerVisible"
      :ossTypes="ossTypes"
      @refresh="refresh"
    ></add-and-update>
  </div>
</template>

<script>
import { listPage, del, updateStatus } from "@/api/file";
import addAndUpdate from "./add-and-update/index.vue";
export default {
  name: "fileconfig",
  components: { addAndUpdate },
  data() {
    return {
      searchform: {
        pageNum: 1,
        pageSize: 5,
        key: null
      },
      ossTypes: [
        { label: "本地", value: "local" },
        { label: "阿里云", value: "alibaba" }
      ],
      total: 0,
      configs: [],
      roleId: null,
      drawerVisible: false
    };
  },
  created() {
    this.listPage();
  },
  methods: {
    async listPage() {
      const { data } = await listPage(this.searchform);
      this.configs = data.data;
      this.searchform.pageNum = data.pageNum;
      this.searchform.pageSize = data.pageSize;
      this.total = data.total;
    },
    addFileConfig() {
      this.drawerVisible = true;
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      this.searchform.pageNum = val;
      this.list(this.searchform);
    },
    handleEdit(index, row) {
      this.$refs["addAndUpdate"].init(row);
      this.$nextTick(() => {
        this.drawerVisible = true;
      });
    },
    handleChange(row) {
      this.$confirm(
        "此操作会导致文件被上传到已启用oss服务器中,请谨慎操作",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      )
        .then(async () => {
          await updateStatus(row.id, row.status);
          this.listPage();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    handleDelete(index, row) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(async () => {
          await del(row.id);
          if (index === 0) {
            this.searchform.pageNum--;
          }
          this.listPage();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    resetForm(formname) {
      this.$refs[formname].resetFields();
    },
    refresh() {
      this.drawerVisible = false;
      this.listPage();
    }
  }
};
</script>

<style lang="scss" scoped>
.fconfig {
  &-container {
    margin: 30px;
  }

  &-search {
    height: 70px;
    padding: 10px 10px;
    border-radius: 5px;
    background-color: #fff;
  }

  &-body {
    border-radius: 5px;
    background-color: #fff;
    margin: 20px 0px;
    padding: 20px 10px;
  }
}
.el-pagination {
  text-align: center;
}
</style>